package com.dzl.brand;

import com.dzl.base.controller.BaseController;
import com.dzl.brand.pojo.WelfareBrand;
import com.dzl.brand.pojo.query.WelfareBrandQuery;
import com.dzl.brand.pojo.vo.WelfareBrandVo;
import com.dzl.brand.service.WelfareBrandService;
import com.dzl.constant.CommonConstants;
import com.dzl.util.EmptyUtils;
import com.dzl.util.JsonResult;
import com.dzl.util.PageUtils;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

/**
 * 品牌信息表(WelfareBrand)表控制层
 * @author caobucao
 * @since 2021-05-19 09:44:40
 */
@Api(value = "平台端-品牌信息管理", tags = "平台端-品牌信息管理")
@RestController
@RequestMapping("welfareBrand")
public class WelfareBrandController extends BaseController {

    @Autowired
    private WelfareBrandService service;

    /**
     * 分页查询品牌信息
     * @date 2021/5/22 10:22
     * @author caobucao
     * @param query  查询条件
     * @return com.dzl.util.PageUtils<com.dzl.brand.pojo.vo.WelfareBrandVo>
     */
    @ApiOperation(value = "平台端-分页查询品牌信息", notes = "平台端-分页查询品牌信息")
    @GetMapping("queryBrandVoListPage")
    public PageUtils<WelfareBrandVo> queryBrandVoListPage(WelfareBrandQuery query) {
        PageInfo<WelfareBrandVo> pageInfo = service.selectBrandVoListPage(query);
        return new PageUtils<>(pageInfo);
    }

    /**
     * 查询所有品牌信息
     * @date 2021/12/21 10:22
     * @author liuguanjun
     */
    @ApiOperation(value = "平台端-查询所有品牌信息", notes = "平台端-查询所有品牌信息")
    @GetMapping("selectAllBrandList")
    public JsonResult<List<WelfareBrand>> selectAllBrandList() {
        List<WelfareBrand> list = service.selectAllBrandList();
        return JsonResult.success(list);
    }

    /**
     * 分页查询品牌信息（过滤已经添加进入供应商的品牌）
     * @date 2021/9/23 0023 10:02
     * @author wangjian
     * @param query 分页查询条件
     * @return com.dzl.util.PageUtils<com.dzl.brand.pojo.vo.WelfareBrandVo>
     */
    /*@GetMapping("selectBrandVoListPageBySupplier")
    public PageUtils<WelfareBrandVo> selectBrandVoListPageBySupplier(WelfareBrandQuery query) {
        PageInfo<WelfareBrandVo> pageInfo = service.selectBrandVoListPage(query);
        return new PageUtils<>(pageInfo);
    }*/

    /**
     * 新增/编辑 品牌
     * @date 2021/5/22 11:17
     * @author caobucao
     * @param welfareBrand  编辑的品牌对象
     * @return com.dzl.util.JsonResult<java.lang.Integer>
     */
    @ApiOperation(value = "平台端-新增/编辑 品牌", notes = "平台端-新增/编辑 品牌")
    @PostMapping("saveBrand")
    public JsonResult<Integer> saveBrand(@RequestBody WelfareBrand welfareBrand) {
        if(EmptyUtils.isEmpty(welfareBrand.getName())) {
            return JsonResult.paramErrWithMsg("请输入品牌名称");
        }
        if(EmptyUtils.isEmpty(welfareBrand.getSort())) {
            return JsonResult.paramErrWithMsg("请输入排序号，排序号不能为空且必须大于0");
        }
        welfareBrand.setUpdater(getAdminInfo().getId());
        welfareBrand.setUpdaterName(getAdminInfo().getUserName());
        welfareBrand.setUpdateTime(new Date());
        if(EmptyUtils.isEmpty(welfareBrand.getId())) {
            //新增
            welfareBrand.setCreater(getAdminInfo().getId());
            return JsonResult.success(service.insertSelective(welfareBrand));
        } else {
            //修改
            return JsonResult.success(service.updateByPrimaryKeySelective(welfareBrand));
        }
    }

    /**
     * 逻辑删除品牌
     * @date 2021/5/22 11:19
     * @author caobucao
     * @param id 要删除的品牌id
     * @return com.dzl.util.JsonResult<java.lang.Integer>
     */
    @ApiOperation(value = "平台端-逻辑删除品牌", notes = "平台端-逻辑删除品牌")
    @DeleteMapping("deleteBrand")
    public JsonResult<Integer> deleteBrand(Integer id) {
        WelfareBrand welfareBrand = new WelfareBrand();
        welfareBrand.setId(id);
        welfareBrand.setUpdater(getAdminInfo().getId());
        welfareBrand.setUpdaterName(getAdminInfo().getUserName());
        welfareBrand.setUpdateTime(new Date());
        welfareBrand.setStatus(CommonConstants.NO);
        Integer result = service.deleteBrand(welfareBrand);
        return JsonResult.success(result);
    }

    /**
     * 品牌下拉框
     * @date 2021/7/13 0013 16:19
     * @author wangjian
     * @param
     * @return com.dzl.util.PageUtils<com.dzl.brand.pojo.vo.WelfareBrandVo>
     */
    @ApiOperation(value = "平台端-品牌下拉框", notes = "平台端-品牌下拉框")
    @GetMapping("queryBrandList")
    public JsonResult<List<WelfareBrand>> queryBrandVoListPage() {
        WelfareBrand welfareBrand = new WelfareBrand();
        welfareBrand.setStatus(CommonConstants.YES);
        return JsonResult.success(service.select(welfareBrand));
    }

}
